For performance and other reasons, object-oriented computer programming languages, such as Java™, may support a set of primitive data types in addition to reference data types. In general, a variable's data type determines the values it may contain, and the operations that may be performed on it. The terms “primitive types” and “reference types” may be used herein to refer to “primitive data types” and “reference data types” respectively. Primitive types may be predefined by the language specification, and may be named using reserved keywords (such as int or float). In some environments, the value of a primitive type may be stored in a location of a fixed size on the stack, such as a four-byte (32-bit) location for int variables, and may not share state with other primitive values. In contrast, a data type for which, when an object of that data type is instantiated, a reference (in effect, a pointer) to a heap location storing the value of the instantiated object is created, is called a reference type. In other environments, the stack-versus-heap distinction between primitive and reference types may not always apply. For example, virtual machines with escape analysis optimization may be able to stack-allocate certain objects of reference types. In general, a small number of primitive types may be supported by the language, while arbitrary numbers of reference types (e.g., classes, interfaces, or arrays, in the case of Java™) may be defined and created by users of the language. It is noted that the distinction between primitive and reference types may be affected somewhat by language features like value types, which may allow user-defined types to behave more like primitives than reference types. For example, objects of values types would not be comparable for identity, only for value.
Some language constructs or frameworks (such as various classes defined in the Java™ Collections framework for representing aggregates like sets, lists, and maps) may be provided strictly for reference types in object-oriented languages. That is, it may not be legal in the language to set up an aggregate of primitive values, or use aggregate features such as iterators directly on primitive values. However, a number of special reference types, called wrapper types, may also be supported in some languages to manually or automatically encapsulate primitive values as objects (e.g., an int value may be encapsulated using the Integer wrapper reference type in Java™, effectively converting a primitive value to a reference to an object with the same numerical value), so that the functionality provided by the frameworks may be extended to primitive values which are suitably wrapped.
The object-oriented programming language may support implicit and explicit data type conversion operations, in which, for example, the value of an expression of a source data type S may be stored in a variable of a target data type T. The following code may represent an example explicit casting conversion operation:
1. // someMethod returns an object of type SourceType2. SourceType s = someClass.someMethod( );3. // explicit cast to convert s to type TargetType4. TargetType t = (TargetType) s;
Several different kinds of type conversion operations may be supported in a given programming language. For example, exhaustive primitive-to-primitive conversions may be supported for numerical primitives, such as a conversion of an int to a short, or a short to an int. However, in many cases, there may be limitations and irregularities in the extent to which conversions from primitive types to reference types (or from reference types to primitive types) are supported in a given version of a programming language. For example, even though it may appear to a programmer that it would be reasonable and meaningful to be able to convert from a reference type R to a primitive type P (e.g., Long to int), on the grounds that it is meaningful to convert from the unwrapped form of R to P (i.e. long to int), the language may not permit such a transformation (i.e., a compile-time error or a run-time error may be thrown).